package com.c2.web.front;

import com.c2.entity.EbComment;
import com.c2.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/comment")
public class CommentfrontController {

    @Autowired
    private CommentService commentService;

    @RequestMapping("/front/list")
    public String list(Model model, @RequestParam(value = "page", defaultValue = "1") int page,
                       @RequestParam(value = "PageSize", defaultValue = "5") int pageSize) {
        System.out.println("进入...CommentController的List");
        //调用service层分页查询分页信息
        List<EbComment> commentList = commentService.listByPage(page, pageSize);

        //绑定最大页数
        int totalPage = commentService.getMaxPage(pageSize);
        //绑定到model
        model.addAttribute("comments", commentList);
        //绑定当前页数
        model.addAttribute("totalPage", totalPage);
        model.addAttribute("curPage", page);

        System.out.println(page);
        System.out.println(totalPage);
        return "guestbook";
    }



    @RequestMapping("front/save")
    public String save(EbComment ebComment) {
        //1.获取表单的值一参数 参数校验
        System.out.println("EbComment==》"+ebComment);
        //2.调用service保存的方法
        Integer result = commentService.save(ebComment);
        if (result < 1) {
            return null;
        }
        return "redirect:/comment/front/list";
    }
}


